using CfgTable;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GameAOT;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_MallActivePanel : UIBaseNode
    {
        #region Template Generate,don't modify
        protected class UIB_UIN_MallActivePanel
        {
            #region ObjectBinding Generate
            public UnityEngine.RectTransform viewContainer { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton buttonFunc { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<UnityEngine.RectTransform>("viewContainer", out var __tbv0);
                this.viewContainer = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("buttonFunc", out var __tbv1);
                this.buttonFunc = __tbv1;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify



        #region fields

        CfgMallType cfgMallType = null;
        UIEntityViewHelper entityHelper = null;
        #endregion fields

        #region properties

        protected UIB_UIN_MallActivePanel ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_MallActivePanel();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.ui.buttonFunc.AddClickEvent(this.OnButtonFunction_Clicked);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {

        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {
            this.cfgMallType = null;
            this.ClearModel();
        }
        #region methonds
        public void Show(CfgMallType mallType)
        {
            this.cfgMallType = mallType;
            this.Show();
            if (string.IsNullOrEmpty(this.cfgMallType.BG))
            {
                LogGame.LogError($"MallType配置表 Id = {this.cfgMallType.Id}，推荐页的BG字段为空！！！");
                return;
            }

            this.ShowModel(this.cfgMallType.BG, this.ui.viewContainer);
        }

        private void ShowModel(string assetPath, Transform parent)
        {
            if (this.entityHelper == null)
                this.entityHelper = UIEntityViewHelper.NewFromPool();
            this.entityHelper.ShowModel(assetPath, parent);
        }

        private void ClearModel()
        {
            this.entityHelper?.Put2Pool();
            this.entityHelper = null;
        }

        private void OnButtonFunction_Clicked()
        {
            //@TODO 跳转逻辑
            FloatNotice.Instance.Show("跳转逻辑尚未开发！！！");
        }

        #endregion methonds
    }
}
